import { createRouter, createWebHistory } from "vue-router";

const Home = () => import("../layout/index.vue");

const routes = [
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login/index.vue"),
  },
  {
    path: "/",
    name: "home",
    component: Home,
    children: [
      {
        path: "/",
        redirect: "/dashboard",
      },
      {
        path: "/dashboard",
        name: "Dashboard",
        component: () => import("../views/dashboard/index.vue"),
        meta: {
          title: "首页",
        },
      },
      {
        path: "/setting",
        name: "Setting",
        component: () => import("../views/home/index.vue"),
        meta: {
          title: "设置",
        },
      },
      {
        path: "/role",
        name: "Role",
        component: () => import("../views/role/index.vue"),
        meta: {
          title: "角色管理",
        },
      },
      {
        path: "/person",
        name: "Person",
        component: () => import("../views/person/index.vue"),
        meta: {
          title: "人员管理",
        },
      },
      {
        path: "/charts",
        name: "Charts",
        component: () => import("../views/charts/index.vue"),
        meta: {
          title: "图表",
        },
      },
      {
        path: "/develops",
        name: "Develops",
        component: () => import("../views/develops/index.vue"),
        meta: {
          title: "开发计划",
        },
      },
      {
        path: "/profiles",
        name: "Profiles",
        component: () => import("../views/profiles/index.vue"),
        meta: {
          title: "个人资料",
        },
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token");
  if (to.path === "/login") {
    next();
  } else {
    if (token) {
      next();
    } else {
      next("/login");
    }
  }
});

router.afterEach((to, from) => {});

export default router;
